manageroles = function() {
	//添加或者修改窗体定义
	var manageRoleAddOrModifyWin = null;
	//选择组织窗体
	var manageRoleChooseOrganWin = null;
	//搜索组织窗体
	var manageRoleSearchWin = null;
	//修改或者搜索FORM
	var manageRoleForm = null;
	return {
		/*--------------------------------------窗体定义--------------------------------------------*/
		panel: function() {
			//角色STORE
			var rolestore = new Store({
				fields: ['id', 'name', 'organ', 'remark'],
				proxy: {
			        type: 'ajax',
			        url: '../admin/managerole.jspx?index',
			        reader: {
			            type: 'json',
			            root: 'datas',
			            totalProperty: 'total'
			        }
			    },
			    autoLoad: false,
			    listeners: {
			    	load: function() {
			    		var sms = Ext.getCmp('manageroleautotree').getChecked();
						for(var i = 0; i < sms.length; i++) {
							sms[i].set('checked', false);
						} 
			    	}
			    }
			});
			
			//默认加载数据
			rolestore.removeAll();
			rolestore.currentPage = 1;
			rolestore.proxy.extraParams = {name: '', organ: '', inside: true};
			rolestore.load();
			
			//角色列表
			var pageToolBar = Ext.create('Ext.PagingToolbar', {
	            store: rolestore,
	            draggable : false,
	            displayInfo: true
	        });
			var rolegrid = new Grid({
				title: '后台角色列表',
				id: 'managerolegrid',
				region: 'center',
				store: rolestore,
				minColumnWidth: 35,
				stripeRows: true,
				bbar : pageToolBar,
				tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						manageroles.refreshRoleAction();
					}
				}, "-", {
					text: '搜索',
					scale: 'medium',
					iconCls: 'icon-search',
					handler: function() {
						manageroles.searchManageroleAction();
					}
				}, "-", {
					text: '添加',
					id: 'manageroleAdd',
					scale: 'medium',
					iconCls: 'icon-add',
					disabled: true,
					handler: function() {
						manageroles.addManageroleAction();
					}
				}, "-", {
					text: '修改',
					id: 'manageroleEdit',
					scale: 'medium',
					iconCls: 'icon-edit',
					disabled: true,
					handler: function() {
						manageroles.modifyManageroleAction();
					}
				}, "-", {
					text: '删除',
					id: 'manageroleRemove',
					scale: 'medium',
					iconCls: 'icon-remove',
					disabled: true,
					handler: function() {
						manageroles.delManageroleAction();
					}
				}],
				columns: [
		             new Ext.grid.RowNumberer(),
		             {header: '序号', dataIndex: 'id', hidden: true},
		             {header: '名称', dataIndex: 'name', width: 200},
		             {header: '所属组织', dataIndex: 'organ', width: 250},
		             {header: '备注说明', dataIndex: 'remark', width: 250}
				],
				listeners: {
					select: function(fn, rec, index, eOpts) {
						manageroles.clickgridManageroleAction(rec);
					}
				}
			});
			
			var autostore = new TreeStore({
				//fields : ['id', 'text'],
		        root: {
		        	id: 0,
		            expanded: true
		        },
		        proxy: {
		            type: 'ajax',
		            url: '../admin/managerole.jspx?indexOFaction',
		            reader: {
		                type: 'json',
		                root: 'children'
		            }
		        }
		    });
			
			var tree = new Tree({
				id: 'manageroleautotree',
		        store: autostore,
		        region: 'east',
		        hideHeaders: true,
		        rootVisible: false,
		        width: '28%',
		        lines: false,
		        disableSelection: true,
		        title: '权限列表',
		        collapsible: true,
		        tbar: [{
					text: '刷新',
					scale: 'medium',
					iconCls: 'icon-refresh',
					handler: function() {
						manageroles.refreshAutoAction();
					}
				}, "-", {
					text: '保存权限',
					id: 'manageroleSave',
					disabled: true,
					scale: 'medium',
					iconCls: 'icon-config',
					handler: function() {
						manageroles.saveManageroleAction();
					}
				}],
		        listeners:{
		            "checkchange": function(node, state) {
		                if (node.get('leaf')) {
		                    //子节点选中   
		                	if (state) {
		                		node.bubble(function(node){
			                        node.set('checked', state);  
			                        return true;   
			                    });
		                	}
		                } else {
		                	if (state) {
		                		node.bubble(function(node){
			                        node.set('checked', state);  
			                        return true;   
			                    });
		                	} else {
		                		node.cascade(function(node){
			                        node.set('checked', state);  
			                        return true;   
			                    });
		                	}
		                }  
		            }
		        }
			});
			
			var panel = new Panel({
				layout: 'border',
				border: false,
				items: [rolegrid, tree]
			});
			return panel;
		},
		
		//添加或者修改窗体
		addOrModifyWin: function() {
			if (manageRoleAddOrModifyWin == null) {
				manageRoleAddOrModifyWin = new Window({
					id: 'manageroleAddOrModifyWin',
				    title: '编辑角色信息',
				    height: 200,
				    width: 400,
				    layout: 'fit',
				    closeAction: "hide",
				    modal: true,
				    items: [{
				    	xtype: 'form',
				    	id: 'manageroleAddOrModifyForm',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'id',
				        	hidden: true
				        }, {
				        	name: 'name',
				        	fieldLabel: '角色名称',
				        	allowBlank: false
				        }, {
				        	xtype: 'textareafield',
				        	name: 'remark',
				        	fieldLabel: '备注说明'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		var fom = this.up('form').getForm();
				        		if (fom.isValid()) {
				        			manageroles.formSubmitAction(fom);
				        		}
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		manageRoleAddOrModifyWin.hide();
				        	}
				        }]
				    }],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		Ext.getCmp('manageroleAddOrModifyForm').getForm().reset();
			        	}
			        }
				});
			}
			manageRoleAddOrModifyWin.show();
		},
		
		//搜索组织窗体
		searchManageroleWin: function() {
			if (manageRoleSearchWin == null) {
				manageRoleSearchWin = new Window({
					title: '搜索角色',
					height: 120,
					width: 400,
					closeAction: 'hide',
					layout: 'fit',
					modal: true,
					items: [{
						xtype: 'form',
				    	id: 'manageroleSearchForm',
				    	frame: false,
						autoHeight: true,
						bodyPadding: 15,
						fieldDefaults: {
				            labelWidth: 75
				        },
				        defaultType: 'textfield',
				        defaults: {
				            anchor: '95%'
				        },
				        items: [{
				        	name: 'name',
				        	fieldLabel: '角色名称'
				        }],
				        buttons: [{
				        	text: '确定',
				        	handler: function() {
				        		manageroles.searchManageroleSureAction();
				        	}
				        }, {
				        	text: '取消',
				        	handler: function() {
				        		manageRoleSearchWin.hide();
				        	}
				        }]
					}],
			        listeners: {
			        	hide: function(fn, eOpts) {
			        		Ext.getCmp('manageroleSearchForm').getForm().reset();
			        	}
			        }
				});
			}
			manageRoleSearchWin.show();
		},
		/*--------------------------------------触发事件定义--------------------------------------*/
		//刷新角色事件
		refreshRoleAction: function() {
			Ext.getCmp('managerolegrid').getStore().load();
		},
		
		//刷新权限事件
		refreshAutoAction: function() {
			Ext.getCmp('manageroleautotree').getStore().load();
			Ext.getCmp('managerolegrid').getSelectionModel().deselectAll();
		},
		
		//添加角色事件
		addManageroleAction: function() {
			manageroles.addOrModifyWin();
		},
		
		//修改角色事件
		modifyManageroleAction: function() {
			var sel = Ext.getCmp('managerolegrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要修改的角色！');
			} else {
				manageroles.addOrModifyWin();
				Ext.getCmp('manageroleAddOrModifyForm').getForm().loadRecord(sel[0]);
			}
		},
		
		//删除角色事件
		delManageroleAction: function() {
			var sel = Ext.getCmp('managerolegrid').getSelectionModel().getSelection();
			if (sel.length < 1) {
				Ext.Msg.alert('提示', '请选择要删除的角色！');
			} else {
				Ext.MessageBox.confirm('确认', '您确认要删除该角色吗？', function(btn) {
					if (btn == "yes") {
						var res = RequestByUrl('../admin/managerole.jspx?remove', {
							id: sel[0].get('id')
						});
						if (res.ok) {
							Ext.getCmp('managerolegrid').getStore().load();
						}
						Ext.Show.msg(res.msg, '');
					}
				});
			}
		},
		
		//保存权限事件
		saveManageroleAction: function() {
			var rec = Ext.getCmp('managerolegrid').getSelectionModel().getSelection();
			if (rec.length < 1) {
				Ext.Msg.alert('提示', '请选择要保存权限的角色！');
			} else {
				var sms = Ext.getCmp('manageroleautotree').getChecked();
				var str = "";
				for (var i = 0; i < sms.length; i++) {
					if (sms[i].get('id') > 0) {
						str = str + sms[i].get('id') + ";";
					}
				}
				var res = RequestByUrl('../admin/managerole.jspx?save', {
					id: rec[0].get('id'),
					str: str
				});
				if (res.success) {
					Ext.Show.msg(res.msg, '');
				}
			}
		},
		
		//表单提交事件
		formSubmitAction: function(fom) {
			var url = '../admin/managerole.jspx?edit';
			var id = fom.findField('id').value;
			if (id == '') {
				url = '../admin/managerole.jspx?add';
			}
			FormSubmit(fom, url, {}, function(obj) {
				Ext.getCmp('managerolegrid').getStore().load();
				manageRoleAddOrModifyWin.hide();
			});
		},
		
		//搜索窗体事件
		searchManageroleAction: function() {
			manageroles.searchManageroleWin();
		},
		
		//搜索确定事件
		searchManageroleSureAction: function() {
			var fom = Ext.getCmp('manageroleSearchForm').getForm();
			var store = Ext.getCmp('managerolegrid').getStore();
			store.removeAll();
			store.currentPage = 1;
			store.proxy.extraParams = {
				name: fom.findField('name').rawValue
			};
			store.load();
			manageRoleSearchWin.hide();
		},
		
		//单击列表触发事件
		clickgridManageroleAction: function(rec) {
			var res = RequestByUrl('../admin/managerole.jspx?indexOFauto', {
				id: rec.get('id')
			});
			var sel = res.msg.split(';'); 
			var store = Ext.getCmp('manageroleautotree').getStore();
			var sms = Ext.getCmp('manageroleautotree').getChecked();
			for(var i = 0; i < sms.length; i++) {
				sms[i].set('checked', false);
			} 
			for (var i = 0; i < sel.length - 1; i++) {
				var cnode = store.getNodeById(parseInt(sel[i]));
				cnode.set('checked', true);
				cnode.bubble(function(node){
	                node.set('checked', true);  
	                return true;   
	            });
			}
		}
	};
}();